#ifndef __PLATFORM_LIB_FILE_H__
#define __PLATFORM_LIB_FILE_H__
#ifdef __cplusplus
extern "C"
{
#endif

#include <sys/uio.h>

#include "pf_common.h"

/******************************************************************************************************/

typedef enum pf_file_lock
{
    PF_FILE_INVALID = -1,  /* use for avoid/detect some errors */
    PF_FILE_UNLOCK  = 0,   /* unlock */
    PF_FILE_RLOCK   = 1,   /* read lock */
    PF_FILE_WLOCK   = 2,   /* write lock */
} pf_file_lock_e;

typedef enum pf_fd_block
{
    PF_FD_INVALID  = -1,  /* use for avoid/detect some errors */
    PF_FD_UNKNOWN  = 0,   /* unknown block or not */
    PF_FD_BLOCK    = 1,   /* fd is block */
    PF_FD_NONBLOCK = 2,   /* fd is non-block */
} pf_fd_block_e;

/*********************************************************************************************
Function Name:  pf_fseek
Description  :  Like fseeko(), seek to file position. Compatible to large file( > 4G ).
Inputs       :  FILE* stream                        : File stream. Must be NOT NULL.
                int64_t offset                      : Offset bytes to position specified by whence argument.
                int whence                          : offset argument relative to.
                                                      SEEK_SET : Relative to the start of the file.
                                                      SEEK_CUR : Relative to the the current position.
                                                      SEEK_END : Relative to the end-of-file.
Outputs      :  return value                        :  0 : Success.
                                                      <0 : Fail. Return -(errno).
ErrorCodes   :  errno                               : Sys call errno from fseeko().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int64_t pf_fseek(FILE* stream, int64_t offset, int whence);

/*********************************************************************************************
Function Name:  pf_seek
Description  :  Like lseek64(), seek to file position. Compatible to large file( > 4G ).
Inputs       :  int fd                              : File descriptor.
                int64_t offset                      : Offset bytes to position specified by whence argument.
                int whence                          : offset argument relative to.
                                                      SEEK_SET : Relative to the start of the file.
                                                      SEEK_CUR : Relative to the the current position.
                                                      SEEK_END : Relative to the end-of-file.
Outputs      :  return value                        :  0 : Success.
                                                      <0 : Fail. Return -(errno).
ErrorCodes   :  errno                               : Sys call errno from lsee64().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int64_t pf_seek(int fd, int64_t offset, int whence);

/*********************************************************************************************
Function Name:  pf_ftell
Description  :  Like ftello(), report file position. Compatible to large file( > 4G ).
Inputs       :  FILE* stream                        : File stream. Must be NOT NULL.
Outputs      :  return value                        : >=0 : Success. return the current offset.
                                                      -1  : Fail. See errno.
ErrorCodes   :  errno                               : Sys call errno from ftello().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int64_t pf_ftell(FILE* stream);

/*********************************************************************************************
Function Name:  pf_tell
Description  :  Like ftello(), report file position. Compatible to large file( > 4G ).
Inputs       :  int fd                              : File descriptor.
Outputs      :  return value                        : >=0 : Success. return the current offset.
                                                      -1  : Fail. See errno.
ErrorCodes   :  errno                               : Sys call errno from lseek64().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int64_t pf_tell(int fd);

/*********************************************************************************************
Function Name:  pf_set_file_lock
Description  :  Sys call fcntl(), disable or enable read/write record lock to file.
                Compatible to large file ( > 4G ).
Inputs       :  int fd                              : File descriptor.
                int64_t offset                      : Staring offset for lock.
                                                      Offset is relative to start of file.
                int64_t len                         : Number bytes for lock.
                pf_file_lock_e lock_mode            : Lock_mode.
                                                      PF_FILE_UNLOCK : Unlock.
                                                      PF_FILE_RLOCK : Read lock.
                                                      PF_FILE_WLOCK : Write lock.
Outputs      :  return value                        :  0 : Success.
                                                      <0 : Fail. Return -(errno).
ErrorCodes   :  errno                               : Sys call errno from fcntl().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_file_lock(int fd,
                            int64_t offset,
                            int64_t len,
                            pf_file_lock_e lock_mode);

/*********************************************************************************************
Function Name:  pf_get_file_lock
Description  :  Sys call fcntl(), get record lock mode.  Compatible to large file ( > 4G ).
Inputs       :  int fd                              : File descriptor.
                int64_t offset                      : Staring offset for lock.
                                                      Offset is relative to start of file.
                int64_t len                         : Number bytes for lock.
Outputs      :  return value                        : lock mode return.
                                                      PF_FILE_UNLOCK  : Unlock.
                                                      PF_FILE_RLOCK   : Read lock.
                                                      PF_FILE_WLOCK   : Write lock.
                                                      PF_FILE_INVALID : Error. See errno.
ErrorCodes   :  errno                               : Sys call errno from fcntl().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern pf_file_lock_e pf_get_file_lock(int fd, int64_t offset, int64_t len);

/*********************************************************************************************
Function Name:  pf_set_nonblock
Description  :  Sys call fcntl(), enable or disable O_NONBLOCK on a file descriptor.
Inputs       :  int fd                        : File descriptor.
                enum PF_OPT opt               : Option value. PF_ON or PF_OFF.
Outputs      :  return value                  : 0  : Success.
                                                <0 : Fail. Return -(errno).
ErrorCodes   :  errno                         : sys call errno from fcntl().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_nonblock(int fd, enum PF_OPT opt);

/*********************************************************************************************
Function Name:  pf_wait_fd
Description  :  Sys call poll(), wait for some event on a file descriptor.
Inputs       :  int fd                             : File descriptor.
                short events                       : Events to wait.
                int32_t timeout                    : Timeout in milliseconds.
                                                     > 0 : Timeout in milliseconds.
                                                       0 : Means return immediately.
                                                     < 0 : Infinite timeout.
Outputs      :  return value                       : > 0 : Success. Event number return.
                                                       0 : Timeout and no event.
                                                      -1 : Fail. See errno.
ErrorCodes   :  errno                              : sys call errno from poll().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_wait_fd(int fd, short events, int32_t timeout);

/*********************************************************************************************
Function Name:  pf_read_n
Description  :  Like sys call read(), synchronously attempt to read up to count bytes from
                file descriptor fd into the buffer starting at buf, until end of file(EOF),
                timeout or other error.
Inputs       :  int fd                             : File descriptor.
                pf_fd_block_e block_type           : File descroptor block type:
                                                     PF_FD_BLOCK    : fd is blocking.
                                                     PF_FD_NONBLOCK : fd is non-blocking.
                void* buf                          : Buffer for reading into. Must be NOT NULL.
                int32_t size                       : Buffer size. Must great than zero.
                int32_t timeout                    : Timeout in milliseconds.
                                                     > 0 : Timeout in milliseconds.
                                                       0 : Means return immediately.
                                                     < 0 : Infinite timeout.
Outputs      :  return value                       : > 0 : Success. the number of bytes read return.
                                                       0 : end of file(EOF).
                                                      -1 : Fail or timeout. See errno.
                void* buf                          : Readed data filled.
ErrorCodes   :  errno                              : sys call errno from read() or poll().
                PF_ETIMEDOUT                       : timeout.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int32_t pf_read_n(int fd,                    
                         pf_fd_block_e block_type, 
                         void* buf,               
                         int32_t size,           
                         int32_t timeout);      

/*********************************************************************************************
Function Name:  pf_write_n
Description  :  Like sys call write(), synchronously write up to count bytes from the buffer
                pointed buf to the file referred to by the file descriptor fd,
                until no more data written, timeout or other error.
Inputs       :  int fd                             : File descriptor.
                pf_fd_block_e block_type           : File descroptor block type:
                                                     PF_FD_BLOCK    : fd is blocking.
                                                     PF_FD_NONBLOCK : fd is non-blocking.
                void* buf                          : Buffer data for writing to fd. Must be NOT NULL.
                int32_t size                       : Buffer size. Must great than zero.
                int32_t timeout                    : Timeout in milliseconds.
                                                     > 0 : Timeout in milliseconds.
                                                       0 : Means return immediately.
                                                     < 0 : Infinite timeout.
Outputs      :  return value                       : > 0 : Success. the number of bytes written return.
                                                       0 : Nothing was written.
                                                      -1 : Fail or timeout. See errno.
ErrorCodes   :  errno                              : sys call errno from write() or poll().
                PF_ETIMEDOUT                       : timeout.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int32_t pf_write_n(int fd,                   
                          pf_fd_block_e block_type, 
                          const void* buf,          
                          int32_t size,             
                          int32_t timeout);         

/*********************************************************************************************
Function Name:  pf_readv_n
Description  :  Like sys call readv(), synchronously reads iovcnt buffers from the file
                associated with the file descriptor fd into the buffers described by iov,
                until end of file(EOF), timeout or other error.
Inputs       :  int fd                             : File descriptor.
                pf_fd_block_e block_type           : File descroptor block type:
                                                     PF_FD_BLOCK    : fd is blocking.
                                                     PF_FD_NONBLOCK : fd is non-blocking.
                const struct iovec* iov            : Multiple buffers for reading into. Must be NOT NULL.
                int iovcnt                         : Buffers count. Must great than zero, and limited 1024.
                int32_t timeout                    : Timeout in milliseconds.
                                                     > 0 : Timeout in milliseconds.
                                                       0 : Means return immediately.
                                                     < 0 : Infinite timeout.
Outputs      :  return value                       : > 0 : Success. the number of bytes read return.
                                                       0 : end of file(EOF).
                                                      -1 : Fail or timeout. See errno.
                const struct iovec* iov            : Readed data filled.
ErrorCodes   :  errno                              : sys call errno from read() or poll().
                PF_ETIMEDOUT                       : timeout.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int32_t pf_readv_n(int fd,                   
                          pf_fd_block_e block_type, 
                          const struct iovec* iov,
                          int iovcnt,
                          int32_t timeout);        

/*********************************************************************************************
Function Name:  pf_writev_n
Description  :  Like sys call writev(), synchronously writes iovcnt buffers of data described
                by iov to  the  file  associated  with  the  file descriptor fd,
                until no more data written, timeout or other error.
Inputs       :  int fd                             : File descriptor.
                pf_fd_block_e block_type           : File descroptor block type:
                                                     PF_FD_BLOCK    : fd is blocking.
                                                     PF_FD_NONBLOCK : fd is non-blocking.
                const struct iovec* iov            : Multiple buffers for writing into. Must be NOT NULL.
                int iovcnt                         : Buffers count. Must great than zero, and limited 1024.
                int32_t timeout                    : Timeout in milliseconds.
                                                     > 0 : Timeout in milliseconds.
                                                       0 : Means return immediately.
                                                     < 0 : Infinite timeout.
Outputs      :  return value                       : > 0 : Success. the number of bytes written return.
                                                       0 : Nothing was written.
                                                      -1 : Fail or timeout. See errno.
ErrorCodes   :  errno                              : sys call errno from write() or poll().
                PF_ETIMEDOUT                       : timeout.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int32_t pf_writev_n(int fd,                  
                           pf_fd_block_e block_type, 
                           const struct iovec* iov,
                           int iovcnt,
                           int32_t timeout);


/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /* #ifndef __PLATFORM_LIB_FILE_H__ */

